﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Service;
using System.Windows;
using System.ServiceModel.Description;

namespace Server
{
    public class Program
    {
        public static List<ServiceHost> services { get; set; }
        public static void Main(string[] arg)
        {
            services = new List<ServiceHost>();
            StartService("ServerOnline");
            StartService("Registration");
            StartService("JoinGame");
            StartService("ListGame");
            StartService("NewGame");
            StartService("Step");
            StartService("CloseGame");
            StartService("GetMsg");
            StartService("SendMsg");
            StartService("GiveUp");

            Console.ReadLine();

            StopService();

        }

        private static void StartService(string ServiceName)
        {
            //1. step
            //Start services
            Uri baseAddress = new Uri("http://localhost:8000/"+ServiceName+"/");

            // Step 2 Create a ServiceHost instance
            Type type = null;
            Type itype = null;
            if (ServiceName == "ServerOnline")
            {
                type = typeof(ServerOnline);
                itype = typeof(IServerOnline);
            }
            else if (ServiceName == "Registration")
            {
                type = typeof(Registration);
                itype = typeof(IRegistration);
            }
            else if (ServiceName == "JoinGame")
            {
                type = typeof(JoinGame);
                itype = typeof(IJoinGame);
            }
            else if (ServiceName == "ListGame")
            {
                type = typeof(ListGame);
                itype = typeof(IListGame);
            }
            else if (ServiceName == "NewGame")
            {
                type = typeof(NewGame);
                itype = typeof(INewGame);
            }
            else if (ServiceName == "Step")
            {
                type = typeof(Step);
                itype = typeof(IStep);
            }
            else if (ServiceName == "CloseGame")
            {
                type = typeof(CloseGame);
                itype = typeof(ICloseGame);
            }
            else if (ServiceName == "GetMsg")
            {
                type = typeof(GetMsg);
                itype = typeof(IGetMsg);
            }
            else if (ServiceName == "SendMsg")
            {
                type = typeof(SendMsg);
                itype = typeof(ISendMsg);
            }
            else if (ServiceName == "GiveUp")
            {
                type = typeof(GiveUp);
                itype = typeof(IGiveUp);
            }
            
            ServiceHost selfHost = new ServiceHost(type, baseAddress);

            try
            {
                // Step 3 Add a service endpoint.
                selfHost.AddServiceEndpoint(itype, new WSHttpBinding(), ServiceName);

                // Step 4 Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5 Start the service.
                selfHost.Open();
                Console.WriteLine("The "+ServiceName+ " service is ready.");

                services.Add(selfHost);
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }

        private static void StopService()
        {
            foreach(var host in services)
            {
                host.Close();
            }
        }
    }
}
